//index.js
var util = require('../../utils/util.js')
const DB = wx.cloud.database()
//获取应用实例
const app = getApp()

let b_class = ["管理", "质量", "内分泌", "5S~7S", "基础护理", "教学", "文学", "其它", "英文"]
let b_obj = [{}, {}, {}, {}, {}, {}, {}, {}, {}]

Page({
  data: {
    //处理导 航 栏的高度
    rpxdata: wx.getStorageSync('rpxdata'),
    navigationBarAndStatusBarHeight: wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight') + 'px',
    navigationBarAndStatusBarAndsearchHeight: wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight') + 60 + 'px',
    navbarData: {
      showCapsule: 0,
      title: "懒小丫图书管理"
    },

    openid: wx.getStorageSync('openid'),
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    DBget: {},
    DBrm: {},
    rpxdata: wx.getStorageSync('rpxdata'),
    //书籍分类
    book_class: {},

    //search_text
    search_text: ""
  },

  search(e) {
    this.setData({
      search_text: e.detail.value
    })
  },

  //扫码信息
  isbn() {
    let that = this
    wx.scanCode({
      onlyFromCamera: true,
      success: function (res) {
        that.setData({
          search_text: res.result
        })
      },
      fail: function () {
        wx.showToast({
          title: '扫码失败请手工输入',
          duration: 2000
        })
      }
    })
  },

  searchClickEvent() {
    wx.navigateTo({
      url: '/pages/bookinfo/bookinfo-search/bookinfo-search?op1=search &op2=bookname &op3=' + this.data.search_text,
    })
  },


  //设置滚动放大效果
  getSelectItem: function (e) {
    var that = this;
    var itemWidth = e.detail.scrollWidth / that.data.DBget.length; //每个商品的宽度
    var scrollLeft = e.detail.scrollLeft; //滚动宽度
    var curIndex = Math.round(scrollLeft / itemWidth) + 1; //通过Math.round方法对滚动大于一半的位置进行进位
    for (var i = 0, len = that.data.DBget.length; i < len; ++i) {
      that.data.DBget[i].selected = false;
    }
    that.data.DBget[curIndex].selected = true;
    that.setData({
      DBget: that.data.DBget,
    });

  },

  onLoad: function () {
    //设置空白栏的高度 
    this.setData({
      navigationBarAndStatusBarAndsearchHeight: wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight') + this.data.rpxdata * 122 + 'px'
    })



    //获取最新上架的数据
    let that = this
    DB.collection("lchbook").orderBy('uptime', 'desc').skip(0).limit(6).get({
      success: function (res) {
        that.setData({
          DBget: res.data
        })
      }
    })

    DB.collection("lchbook").orderBy('jynum', 'desc').skip(0).limit(3).get({
      success: function (res) {
        that.setData({
          DBrm: res.data
        })
      }
    })

    //    book_class: ["管理", "质量", "内分泌", "5S~7S", "基础护理", "教学", "文学", "其它", "英文"]

    for (let i = 0; i < 9; i++) {
      DB.collection("lchbook").orderBy('uptime' , 'desc').where({
        bookclass: b_class[i]
      }).count({
        success: function (res) {
          b_obj[i].booknum = res.total
        }
      })
      b_obj[i].bookclass = b_class[i]
      DB.collection("lchbook").where({
        bookclass: b_class[i]
      }).orderBy('uptime' , 'desc').skip(0).limit(3).get({
        success: (res) => {
          b_obj[i].pic_1 = res.data[0].bookpic
          b_obj[i].pic_2 = res.data[1].bookpic
          b_obj[i].pic_3 = res.data[2].bookpic
        }
      })
    }
    

  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
    //获取用户信息完成
  },

  onShow: function () {
    this.setData({book_class:b_obj})
    
    //tabbar显示
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        tabIndex: wx.getStorageSync('tabIndex')
      })
    }
  }
})